<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="单据编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="单据编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商" prop="supplierName"  />
      <el-table-column label="单据编号" prop="orderNo" />
      <el-table-column label="商品信息" prop="productName"  />
      <el-table-column label="单据日期" prop="createTime"  />
      <el-table-column label="操作员" prop="createBy"  />
      <el-table-column label="金额合计" prop="totalAmount"  />
      <el-table-column label="待付金额" prop="amount"  />
      <el-table-column label="已付定金" prop="earnest"  />
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <span style="color: cornflowerblue" v-if="scope.row.status===0">待审核</span>
          <span style="color: green" v-if="scope.row.status===1">已审核</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配置对话框 -->
    <el-dialog title="添加采购入库单" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form"  label-width="100px" :inline="true" style="width: 100%;padding: 20px">
<!--        <el-form-item label="供应商" prop="属性">-->
<!--          <el-select v-model="form.supplierId" placeholder="请选择供应商" size="mini" />-->
<!--        </el-form-item>-->
        &emsp;&emsp;&emsp;
        <el-form-item>
          <el-button icon="el-icon-search" @click="showCgorderDialog" size="mini">关联采购订单</el-button>
        </el-form-item>

        <el-table   :data="selectCgorderList" >
          <el-table-column label="供应商" prop="supplierName"  />
          <el-table-column label="单号" prop="orderNo"  />
          <el-table-column label="单据日期" prop="createTime" />
          <el-table-column label="操作员" prop="createBy"  />
          <el-table-column label="总价" prop="amount" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.amount" @change="numChange(scope.row.amount)" placeholder="" size="mini" style="width: 100px"/>
            </template>
          </el-table-column>

          <el-table-column label="优惠" prop="reduced"  />
          <el-table-column label="定金" prop="earnest"  />
          <el-table-column label="状态" prop="status" >
            <template slot-scope="scope">
              <span style="color: red" v-if="scope.row.status===1">已审核</span>
            </template>
          </el-table-column>
        </el-table>
        <br/>
        <div style="width: 100%">
          <el-input  type="textarea" v-model="form.属性" placeholder="订单备注" />
        </div>
        <br/>
        <el-form-item label="结算账户" prop="属性">
          <el-select v-model="form.accountId" placeholder="" size="mini">
            <el-option
              v-for="item in accountList"
              :key="item.id"
              :label="item.name+item.sn"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单总额" prop="属性">
          <el-input v-model="form.totalAmount"  placeholder="" size="mini"/>
        </el-form-item>
        <el-form-item label="优惠金额" prop="属性">
          <el-input v-model="form.discountAmount"  placeholder="" size="mini"/>
        </el-form-item>
        <el-form-item label="扣除定金" prop="属性">
          <el-input v-model="form.depositAmount"  placeholder="" size="mini"/>
        </el-form-item>
        <el-form-item label="本次付款" prop="属性">
          <el-input v-model="form.payamount" placeholder="" size="mini"/>
        </el-form-item>
        <el-form-item label="本次欠款" prop="属性">
          <el-input v-model="form.debtAmount" placeholder="" size="mini"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button type="primary" @click="submitFormAndCheck">添加并审核</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 选择采购单的弹窗 -->
    <el-dialog title="选择采购订单" :visible.sync="goodsListOpen" width="1000px" append-to-body>

      <el-table  ref="cgorderTable"  :data="goodsList" @selection-change="handleCgorderSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="供应商" prop="supplierName"  />
        <el-table-column label="单号" prop="orderNo"  />
        <el-table-column label="单据日期" prop="createTime" />
        <el-table-column label="操作员" prop="createBy"  />
        <el-table-column label="总价" prop="amount"  />
        <el-table-column label="优惠" prop="reduced"  />
        <el-table-column label="定金" prop="earnest"  />
        <el-table-column label="状态" prop="status" >
          <template slot-scope="scope">
            <span style="color: red" v-if="scope.row.status===1">已审核</span>
        </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sum">确定</el-button>
        <el-button @click="cgorderListDialogCancel">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {listOrder, listOrderByStatus} from "@/api/system/order";
import {listAccount} from "@/api/sale/account";
import {addStoreOrder, listStoreOrder} from "@/api/sale/storeOrder";

export default {
  name: "cgstore",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      accountList:[],
      showSearch:true,
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      //商品列表
      goodsList: [],
      //选中的商品
      selectCgorderList:[],
      //第一个弹窗
      open:false,
      //第二个弹窗
      goodsListOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo:""
      },
      // 表单参数
      form: {
        orderNo:"", //订单号
        accountId:"", //结算账户ID
        depositAmount:0, //定金
        discountAmount:0,//优惠金额
        payamount:0,//付款金额
        totalAmount:0,//总额
        debtAmount:0,//欠款
        remark:"",//备注
        storeOrderStatus:0,//入库单状态0：待审核，1：审核通过
        supplierId:0,

      },
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    numChange(){
      this.sum();
    },
    sum(){
      //每次重选先重置为0
      this.form.totalAmount=0;
      //计算选中数据的各项指标数据和
      for(let i in this.selectCgorderList){
        this.form.supplierId = this.selectCgorderList[i].supplierId;
        this.form.totalAmount += this.selectCgorderList[i].amount;
        this.form.discountAmount += this.selectCgorderList[i].reduced;
        this.form.depositAmount += this.selectCgorderList[i].earnest;
        this.form.payamount += this.selectCgorderList[i].amount - this.selectCgorderList[i].earnest -this.selectCgorderList[i].reduced;
      }
      console.log(this.form.totalAmount)
      this.goodsListOpen = false;
    },
    cancel() {
      this.open = false;
    },
    cgorderListDialogCancel(){
      this.goodsListOpen = false;
      this.selectCgorderList = [];
      this.$refs.cgorderTable.clearSelection();
    },
    showCgorderDialog(){
      this.goodsListOpen = true;
      //查询所有状态是已审核的采购主订单
      listOrderByStatus().then(response =>{
        this.goodsList = response.rows;
        this.total = response.total;
        this.goodsListOpen = true;
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = false;
      listStoreOrder(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleCgorderSelectionChange(selection){
      this.selectCgorderList = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      //查询所有本公司账户
      listAccount().then(response=>{
        this.accountList = response.rows;
        this.total = response.total;
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.form['selectCgorderList'] = this.selectCgorderList;
      this.form.storeOrderStatus=0;
      console.log(this.form);
      //发送请求，保存订单
      addStoreOrder(this.form).then(response=> {
        console.log("提交");
        this.getList();
      })
      this.open = false;
    },
    //提交并审核
    submitFormAndCheck(){
      this.form['selectCgorderList'] = this.selectCgorderList;
      this.form.storeOrderStatus=1;
      console.log(this.form);
      addStoreOrder(this.form).then(response=> {
        console.log("提交");
        this.getList();
      })
      this.open = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
